package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chenenyu.router.Router;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.request.RegisterRequest;
import sz.szsmk.citizencard.model.network.bean.response.UserBean;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.RegistBySmsMvpView;
import sz.szsmk.citizencard.mvp.presenter.RegistBySmsPresenter;
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity;
import sz.szsmk.citizencard.util.ACache;

/**
 * author : hyt
 * date : 2019/8/30
 * description : 短信注册后的页面  输入密码  xml用的是 设置密码/重置密码 SettingPsdActivity
 */
public class RegistBySmsActivity extends AppBaseActivity implements RegistBySmsMvpView {
    @Inject
    RegistBySmsPresenter presenter;

    LinearLayout set_psd_reset;

    EditText setpsd_et_setpsd;

    EditText setpsd_et_surepsd;

    private String mobile;
    private String sms_code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.setpsd_activity);

        set_psd_reset = findViewById(R.id.set_psd_reset);

        setpsd_et_setpsd = findViewById(R.id.setpsd_et_setpsd);

        setpsd_et_surepsd = findViewById(R.id.setpsd_et_surepsd);;


        findViewById(R.id.setpsd_sure).setOnClickListener(this::onClick);
        findViewById(R.id.back).setOnClickListener(this::onClick);


        set_psd_reset.setVisibility(View.GONE);

        mobile = getIntent().getStringExtra("mobile");
        sms_code = getIntent().getStringExtra("sms_code");

    }

    @Override
    protected CharSequence getTopTitle() {
        return null;
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{ presenter };
    }


    public void onClick(View v){
        int id = v.getId();
        if (id == R.id.setpsd_sure) {
            if (!TextUtils.isEmpty(setpsd_et_setpsd.getText().toString().trim())) {
                if (setpsd_et_setpsd.getText().toString().trim().equals(setpsd_et_surepsd.getText().toString().trim())) {
                    RegisterRequest registerRequest = new RegisterRequest();
                    registerRequest.setMobile(mobile);
                    registerRequest.setSms_code(sms_code);
                    registerRequest.setPassword(setpsd_et_surepsd.getText().toString().trim());
                    presenter.registerAndLogin(registerRequest);
                } else {
                    Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
            }
        } else if (id == R.id.back) {
            finish();
        }
    }


    @Override
    public void registerAndLoginResult(UserBean userBean) {
        ACache.get(DaggerApplication.getIns()).put("access_token",userBean.getAccess_token());
        ACache.get(DaggerApplication.getIns()).put("refresh_token",userBean.getRefresh_token());
        Router.build("main").go(this);
        finish();
    }

    @Override
    public void registerFail(UserBean userBean) {

    }
}
